package io.renren.modules.generator.service.impl;

import io.renren.constant.DeviceFinal;
import io.renren.modules.generator.dao.DeviceSiteDao;
import io.renren.modules.generator.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.generator.dao.DeviceWarningQuotaDao;
import io.renren.modules.generator.service.DeviceWarningQuotaService;


@Service("deviceWarningQuotaService")
public class DeviceWarningQuotaServiceImpl extends ServiceImpl<DeviceWarningQuotaDao, DeviceWarningQuotaEntity> implements DeviceWarningQuotaService {

    @Autowired
    private DeviceWarningQuotaDao deviceWarningQuotaDao;
    @Autowired
    private DeviceSiteDao deviceSiteDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DeviceWarningQuotaEntity> page = this.page(
                new Query<DeviceWarningQuotaEntity>().getPage(params),
                new QueryWrapper<DeviceWarningQuotaEntity>()
        );

        return new PageUtils(page);
    }


    public DeviceWarningQuotaEntity getDeviceWarningQuotaEntity(Object quotaEntity,BigDecimal bigDecimal){
        QueryWrapper<DeviceSiteEntity> queryWrapper = new QueryWrapper<>();
        DeviceWarningQuotaEntity deviceWarningQuotaEntity = null;
        try{
            if(quotaEntity instanceof DeviceHourEntity){
                DeviceHourEntity deviceHourEntity = (DeviceHourEntity) quotaEntity;
                queryWrapper.lambda().eq(DeviceSiteEntity::getDeviceCode,deviceHourEntity.getDeviceEncrypt());
                DeviceSiteEntity deviceSiteEntity = deviceSiteDao.selectOne(queryWrapper);
                String deviceAdress = Objects.nonNull(deviceSiteEntity) ? deviceSiteEntity.getSiteName() : null;
                deviceWarningQuotaEntity =
                        DeviceWarningQuotaEntity.builder().deviceEncrypt(deviceHourEntity.getDeviceEncrypt())
                                .deviceCode(deviceHourEntity.getDeviceCode())
                                .deviceIndexName(DeviceFinal.allQuotaMap.get(deviceHourEntity.getDeviceCode()))
                                .createTime(deviceHourEntity.getCreateTime())
                                .quotaValue(String.valueOf(bigDecimal))
                                .rangeEnum(DeviceFinal.hour_warning)
                                .realValue(deviceHourEntity.getDeviceValue())
                                .createAddress(deviceAdress).build();
            }else if(quotaEntity instanceof DeviceDayEntity){
                DeviceDayEntity deviceDayEntity = (DeviceDayEntity) quotaEntity;
                queryWrapper.lambda().eq(DeviceSiteEntity::getDeviceCode,deviceDayEntity.getDeviceEncrypt());
                DeviceSiteEntity deviceSiteEntity = deviceSiteDao.selectOne(queryWrapper);
                String deviceAdress = Objects.nonNull(deviceSiteEntity) ? deviceSiteEntity.getSiteName() : null;
                deviceWarningQuotaEntity =
                        DeviceWarningQuotaEntity.builder().deviceEncrypt(deviceDayEntity.getDeviceEncrypt())
                                .deviceCode(deviceDayEntity.getDeviceCode())
                                .deviceIndexName(DeviceFinal.allQuotaMap.get(deviceDayEntity.getDeviceCode()))
                                .createTime(deviceDayEntity.getCreateTime())
                                .quotaValue(String.valueOf(bigDecimal))
                                .rangeEnum(DeviceFinal.day_warning)
                                .realValue(deviceDayEntity.getDeviceValue())
                                .createAddress(deviceAdress).build();
            }else if(quotaEntity instanceof DeviceYearEntity){
                DeviceYearEntity deviceYearEntity = (DeviceYearEntity) quotaEntity;
                queryWrapper.lambda().eq(DeviceSiteEntity::getDeviceCode,deviceYearEntity.getDeviceEncrypt());
                DeviceSiteEntity deviceSiteEntity = deviceSiteDao.selectOne(queryWrapper);
                String deviceAdress = Objects.nonNull(deviceSiteEntity) ? deviceSiteEntity.getSiteName() : null;
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat df = new SimpleDateFormat("yyyy-00-00 00:00:00");
                String nowDay = df.format(calendar.getTime());
                Date date = df.parse(df.format(calendar.getTime()));
                deviceWarningQuotaEntity =
                        DeviceWarningQuotaEntity.builder().deviceEncrypt(deviceYearEntity.getDeviceEncrypt())
                                .deviceCode(deviceYearEntity.getDeviceCode())
                                .deviceIndexName(DeviceFinal.allQuotaMap.get(deviceYearEntity.getDeviceCode()))
                                .createTime(date)
                                .quotaValue(String.valueOf(bigDecimal))
                                .rangeEnum(DeviceFinal.day_warning)
                                .realValue(deviceYearEntity.getDeviceValue())
                                .createAddress(deviceAdress).build();
            }else if(quotaEntity instanceof DeviceAllEntity){
                DeviceAllEntity deviceAllEntity = (DeviceAllEntity) quotaEntity;
                queryWrapper.lambda().eq(DeviceSiteEntity::getDeviceCode,deviceAllEntity.getDeviceEncrypt());
                DeviceSiteEntity deviceSiteEntity = deviceSiteDao.selectOne(queryWrapper);
                String deviceAdress = Objects.nonNull(deviceSiteEntity) ? deviceSiteEntity.getSiteName() : null;
                deviceWarningQuotaEntity =
                        DeviceWarningQuotaEntity.builder().deviceEncrypt(deviceAllEntity.getDeviceEncrypt())
                                .deviceCode(deviceAllEntity.getDeviceCode())
                                .deviceIndexName(DeviceFinal.allQuotaMap.get(deviceAllEntity.getDeviceCode()))
                                .createTime(deviceAllEntity.getCreateTime())
                                .quotaValue(String.valueOf(bigDecimal))
                                .rangeEnum(DeviceFinal.actual_warning)
                                .realValue(deviceAllEntity.getDeviceValue())
                                .createAddress(deviceAdress).build();
            }
            if(Objects.nonNull(deviceWarningQuotaDao)){//如果存在相同时间值的预警则直接先删除，不做各种逻辑判断
                QueryWrapper<DeviceWarningQuotaEntity> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.lambda().eq(DeviceWarningQuotaEntity::getDeviceEncrypt,deviceWarningQuotaEntity.getDeviceEncrypt())
                        .eq(DeviceWarningQuotaEntity::getDeviceCode,deviceWarningQuotaEntity.getDeviceCode())
                        .eq(DeviceWarningQuotaEntity::getCreateTime,deviceWarningQuotaEntity.getCreateTime());
                deviceWarningQuotaDao.delete(queryWrapper1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return deviceWarningQuotaEntity;
    }

    @Override
    public void putHourWarningQuota(DeviceHourEntity deviceHourEntity) {
        BigDecimal deviceValue = new BigDecimal(deviceHourEntity.getDeviceValue());
        DeviceWarningQuotaEntity deviceWarningQuotaEntity = null;


        boolean gasDeviceEncryptFlag = false;//是否是挥发性有机物在线监测（VOCs）气体设备
        for (int i = 0; i < DeviceFinal.device_gas_code_arr.length; i++) {
            if(DeviceFinal.device_gas_code_arr[i].equals(deviceHourEntity.getDeviceEncrypt())){
                gasDeviceEncryptFlag = true;
            }
        }
        if(gasDeviceEncryptFlag){//非甲烷总烃预警
            if(deviceHourEntity.getDeviceCode().equals(DeviceFinal.GAS_Quota_a24088_Rtd)
                    && deviceValue.compareTo(DeviceFinal.HOUR_GAS_Quota_a24088_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceHourEntity,DeviceFinal.HOUR_GAS_Quota_a24088_Rtd);
            }else if(deviceHourEntity.getDeviceCode().equals(DeviceFinal.GAS_Quota_a05002_Rtd)
                    && deviceValue.compareTo(DeviceFinal.HOUR_GAS_Quota_a05002_Rtd) == -1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceHourEntity,DeviceFinal.HOUR_GAS_Quota_a05002_Rtd);
            }else if(deviceHourEntity.getDeviceCode().equals(DeviceFinal.GAS_Quota_a24087_Rtd)
                    && deviceValue.compareTo(DeviceFinal.HOUR_GAS_Quota_a24087_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceHourEntity,DeviceFinal.HOUR_GAS_Quota_a24087_Rtd);
            }

        }

        boolean smokeDeviceEncryptFlag = false;//是否是烟气在线监测设备
        for (int i = 0; i < DeviceFinal.device_smokeonline_code_arr.length; i++) {
            if(DeviceFinal.device_smokeonline_code_arr[i].equals(deviceHourEntity.getDeviceEncrypt())){
                smokeDeviceEncryptFlag = true;
            }
        }
        if(smokeDeviceEncryptFlag){//烟气在线监测 预警
            if(deviceHourEntity.getDeviceCode().equals(DeviceFinal.SMOKE_02_Rtd)
                    && deviceValue.compareTo(DeviceFinal.HOUR_SMOKE_02_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceHourEntity,DeviceFinal.HOUR_SMOKE_02_Rtd);
            }else if(deviceHourEntity.getDeviceCode().equals(DeviceFinal.SMOKE_03_Rtd)
                    && deviceValue.compareTo(DeviceFinal.HOUR_SMOKE_03_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceHourEntity,DeviceFinal.HOUR_SMOKE_03_Rtd);
            }else if(deviceHourEntity.getDeviceCode().equals(DeviceFinal.SMOKE_01_Rtd)
                    && deviceValue.compareTo(DeviceFinal.HOUR_SMOKE_01_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceHourEntity,DeviceFinal.HOUR_SMOKE_01_Rtd);
            }
        }


        boolean waterDeviceEncryptFlag = false;//是否是聚光水质设备
        for (int i = 0; i < DeviceFinal.device_water_code_arr.length; i++) {
            if(DeviceFinal.device_water_code_arr[i].equals(deviceHourEntity.getDeviceEncrypt())){
                waterDeviceEncryptFlag = true;
            }
        }
        if(waterDeviceEncryptFlag){//水质设备 预警
            if(deviceHourEntity.getDeviceCode().equals(DeviceFinal.WATER_Quota_w21003_Rtd)
                    && deviceValue.compareTo(DeviceFinal.HOUR_WATER_Quota_w21003_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceHourEntity,DeviceFinal.HOUR_WATER_Quota_w21003_Rtd);
            }else if(deviceHourEntity.getDeviceCode().equals(DeviceFinal.WATER_Quota_w21011_Rtd)
                    && deviceValue.compareTo(DeviceFinal.HOUR_WATER_Quota_w21011_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceHourEntity,DeviceFinal.HOUR_WATER_Quota_w21011_Rtd);
            }else if(deviceHourEntity.getDeviceCode().equals(DeviceFinal.WATER_Quota_w01001_Rtd)
                    && deviceValue.compareTo(DeviceFinal.HOUR_WATER_Quota_w01001_Rtd_more) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceHourEntity,DeviceFinal.HOUR_WATER_Quota_w01001_Rtd_more);
            }else if(deviceHourEntity.getDeviceCode().equals(DeviceFinal.WATER_Quota_w01001_Rtd)
                    && deviceValue.compareTo(DeviceFinal.HOUR_WATER_Quota_w01001_Rtd_less) == -1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceHourEntity,DeviceFinal.HOUR_WATER_Quota_w01001_Rtd_less);
            }
        }

        if(Objects.nonNull(deviceWarningQuotaEntity)){
            deviceWarningQuotaDao.insert(deviceWarningQuotaEntity);
        }
    }

    @Override
    public void putDayWarningQuota(DeviceDayEntity deviceDayEntity) {
        BigDecimal deviceValue = new BigDecimal(deviceDayEntity.getDeviceValue());
        DeviceWarningQuotaEntity deviceWarningQuotaEntity = null;


        boolean tspDeviceEncryptFlag = false;//是否是天瑞大气粉尘在线监测（TSP）设备
        for (int i = 0; i < DeviceFinal.device_tsp_code_arr.length; i++) {
            if(DeviceFinal.device_tsp_code_arr[i].equals(deviceDayEntity.getDeviceEncrypt())){
                tspDeviceEncryptFlag = true;
            }
        }
        if(tspDeviceEncryptFlag){//tsp预警
            if(deviceDayEntity.getDeviceCode().equals(DeviceFinal.TSP_Quota_a34001_Rtd)
                    && deviceValue.compareTo(DeviceFinal.DAY_TSP_Quota_a34001_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceDayEntity,DeviceFinal.DAY_TSP_Quota_a34004_Rtd);
            }else if(deviceDayEntity.getDeviceCode().equals(DeviceFinal.TSP_Quota_a34004_Rtd)
                    && deviceValue.compareTo(DeviceFinal.DAY_TSP_Quota_a34004_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceDayEntity,DeviceFinal.DAY_TSP_Quota_a34004_Rtd);
            }
        }

        boolean pm25DeviceEncryptFlag = false;//是否是天瑞大气颗粒物在线监测（PM2.5）设备
        for (int i = 0; i < DeviceFinal.device_pm25_code_arr.length; i++) {
            if(DeviceFinal.device_pm25_code_arr[i].equals(deviceDayEntity.getDeviceEncrypt())){
                pm25DeviceEncryptFlag = true;
            }
        }
        if(pm25DeviceEncryptFlag){//pm25预警
            if(deviceDayEntity.getDeviceCode().equals(DeviceFinal.PM_25_Quota_a34004_Avg)
                    && deviceValue.compareTo(DeviceFinal.DAY_PM_25_Quota_a34004_Avg) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceDayEntity,DeviceFinal.DAY_PM_25_Quota_a34004_Avg);
            }
        }

        boolean gasDeviceEncryptFlag = false;//是否是挥发性有机物在线监测（VOCs）设备
        for (int i = 0; i < DeviceFinal.device_gas_code_arr.length; i++) {
            if(DeviceFinal.device_gas_code_arr[i].equals(deviceDayEntity.getDeviceEncrypt())){
                gasDeviceEncryptFlag = true;
            }
        }
        if(gasDeviceEncryptFlag){//pm25预警
            if(deviceDayEntity.getDeviceCode().equals(DeviceFinal.GAS_Quota_a05002_Rtd)
                    && deviceValue.compareTo(DeviceFinal.DAY_GAS_Quota_a05002_Rtd) == -1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceDayEntity,DeviceFinal.DAY_GAS_Quota_a05002_Rtd);
            }
        }

        boolean smokeDeviceEncryptFlag = false;//是否是天瑞大气颗粒物在线监测（PM2.5）设备
        for (int i = 0; i < DeviceFinal.device_smokeonline_code_arr.length; i++) {
            if(DeviceFinal.device_smokeonline_code_arr[i].equals(deviceDayEntity.getDeviceEncrypt())){
                smokeDeviceEncryptFlag = true;
            }
        }
        if(smokeDeviceEncryptFlag){//pm25预警
            if(deviceDayEntity.getDeviceCode().equals(DeviceFinal.SMOKE_02_Rtd)
                    && deviceValue.compareTo(DeviceFinal.DAY_SMOKE_02_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceDayEntity,DeviceFinal.DAY_SMOKE_02_Rtd);
            }else if(deviceDayEntity.getDeviceCode().equals(DeviceFinal.SMOKE_03_Rtd)
                    && deviceValue.compareTo(DeviceFinal.DAY_SMOKE_03_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceDayEntity,DeviceFinal.DAY_SMOKE_03_Rtd);
            }
        }

        if(Objects.nonNull(deviceWarningQuotaEntity)){
            deviceWarningQuotaDao.insert(deviceWarningQuotaEntity);
        }
    }

    @Override
    public void putYearWarningQuota(DeviceYearEntity deviceYearEntity) {
        BigDecimal deviceValue = new BigDecimal(deviceYearEntity.getDeviceValue());
        DeviceWarningQuotaEntity deviceWarningQuotaEntity = null;


        boolean tspDeviceEncryptFlag = false;//是否是天瑞大气粉尘在线监测（TSP）设备
        for (int i = 0; i < DeviceFinal.device_tsp_code_arr.length; i++) {
            if(DeviceFinal.device_tsp_code_arr[i].equals(deviceYearEntity.getDeviceEncrypt())){
                tspDeviceEncryptFlag = true;
            }
        }
        if(tspDeviceEncryptFlag){//tsp预警
            if(deviceYearEntity.getDeviceCode().equals(DeviceFinal.TSP_Quota_a34001_Rtd)
                    && deviceValue.compareTo(DeviceFinal.YEAR_TSP_Quota_a34001_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceYearEntity,DeviceFinal.YEAR_TSP_Quota_a34001_Rtd);
            }else if(deviceYearEntity.getDeviceCode().equals(DeviceFinal.TSP_Quota_a34004_Rtd)
                    && deviceValue.compareTo(DeviceFinal.YEAR_TSP_Quota_a34004_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceYearEntity,DeviceFinal.YEAR_TSP_Quota_a34004_Rtd);
            }
        }

        boolean pm25DeviceEncryptFlag = false;//是否是天瑞大气颗粒物在线监测（PM2.5）设备
        for (int i = 0; i < DeviceFinal.device_pm25_code_arr.length; i++) {
            if(DeviceFinal.device_pm25_code_arr[i].equals(deviceYearEntity.getDeviceEncrypt())){
                pm25DeviceEncryptFlag = true;
            }
        }
        if(pm25DeviceEncryptFlag){//pm25预警
            if(deviceYearEntity.getDeviceCode().equals(DeviceFinal.PM_25_Quota_a34004_Avg)
                    && deviceValue.compareTo(DeviceFinal.YEAR_PM_25_Quota_a34004_Avg) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceYearEntity,DeviceFinal.YEAR_PM_25_Quota_a34004_Avg);
            }
        }

        boolean smokeDeviceEncryptFlag = false;//是否是天瑞大气颗粒物在线监测（PM2.5）设备
        for (int i = 0; i < DeviceFinal.device_smokeonline_code_arr.length; i++) {
            if(DeviceFinal.device_smokeonline_code_arr[i].equals(deviceYearEntity.getDeviceEncrypt())){
                smokeDeviceEncryptFlag = true;
            }
        }
        if(smokeDeviceEncryptFlag){//pm25预警
            if(deviceYearEntity.getDeviceCode().equals(DeviceFinal.SMOKE_02_Rtd)
                    && deviceValue.compareTo(DeviceFinal.YEAR_SMOKE_02_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceYearEntity,DeviceFinal.YEAR_SMOKE_02_Rtd);
            }else if(deviceYearEntity.getDeviceCode().equals(DeviceFinal.SMOKE_03_Rtd)
                    && deviceValue.compareTo(DeviceFinal.YEAR_SMOKE_03_Rtd) == 1){
                deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceYearEntity,DeviceFinal.DAY_SMOKE_03_Rtd);
            }
        }

        if(Objects.nonNull(deviceWarningQuotaEntity)){
            deviceWarningQuotaDao.insert(deviceWarningQuotaEntity);
        }
    }

    @Override
    public void putActualWarningQuota(DeviceAllEntity deviceAllEntity) {
        BigDecimal deviceValue = new BigDecimal(deviceAllEntity.getDeviceValue());
        for (int i = 0; i < DeviceFinal.device_doppler_code_arr.length; i++) {
            if(
                    DeviceFinal.device_doppler_code_arr[i].equals(deviceAllEntity.getDeviceEncrypt())
                    && deviceAllEntity.getDeviceCode().equals(DeviceFinal.SWEAGE_Ch1)
                    && deviceValue.compareTo(DeviceFinal.ACTUAL_SWEAGE_Ch1) == 1
                ){
                DeviceWarningQuotaEntity deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceAllEntity,DeviceFinal.ACTUAL_SWEAGE_Ch1);
                deviceWarningQuotaDao.insert(deviceWarningQuotaEntity);
            }
        }
        for (int i = 0; i < DeviceFinal.device_huohuam_code_arr.length; i++) {
            if(
                    DeviceFinal.device_huohuam_code_arr[i].equals(deviceAllEntity.getDeviceEncrypt())
                            && deviceAllEntity.getDeviceCode().equals(DeviceFinal.huohua_Czsgzhyq_Hhs_Rtd)
                            && deviceValue.compareTo(DeviceFinal.ACTUAL_Czsgzhyq_Hhs_Rtd) == 0
            ){
                DeviceWarningQuotaEntity deviceWarningQuotaEntity =
                        getDeviceWarningQuotaEntity(deviceAllEntity,DeviceFinal.ACTUAL_Czsgzhyq_Hhs_Rtd);
                deviceWarningQuotaDao.insert(deviceWarningQuotaEntity);
            }
        }
    }


}